package com.quality.servlet;

import com.quality.util.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;

import com.quality.util.JdbcUtils;

public class DownloadServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //1.获取下载文件的名称
        String FileID=request.getParameter("fileID");

        JdbcUtils jdbcUtils = new JdbcUtils();

        Map<String,Object> map=new HashMap<String, Object>();

        try {
            String sql = "select * from [MES].[dbo].[Tab_MES_BaseManage_FileControl] where id='" + FileID + "' ";
            jdbcUtils.getConnection();
            map = jdbcUtils.findSimpleResult(sql, null);
            System.out.println("sql->"+sql);


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtils.relaseConn();
        }

        String filename = "";
        String fileUpAddress = "";
        if(!map.isEmpty()){
            filename = map.get("FileName").toString();
            fileUpAddress = map.get("FileUp_Address").toString();
        }

        InputStream in = null;
        ServletOutputStream out = null;

        try{
            String url = "smb:" + fileUpAddress;
            url = url.replace("\\","/");
            SmbFile remoteFile = new SmbFile(url);
            if(remoteFile==null){
                System.out.println("共享文件不存在");
                return;
            }



            //*告诉客户端这个文件不是解析 而是以附件的形式下载
            response.setHeader("Content-Disposition", "attachment;filename="+filename);
            //2.获取文件的绝对路径
            //String path= fileUpAddress;//this.getServletContext().getRealPath(fileUpAddress);
            //3.获得该文件的输入流
            //FileInputStream in=new FileInputStream(path);
            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
            //获得输出流---通过response获得的输出流  用于向客户端写内容
            out=response.getOutputStream();
            //拷贝文件
            int len=0;
            byte [] buffer=new byte[1024];

            while((len=in.read(buffer))>0) {
                out.write(buffer, 0, len);
            }
            in.close();
            out.close();
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    public void smbGet(String remoteUrl,String localDir) {
        InputStream in = null;
        OutputStream out = null;
        try {
            SmbFile remoteFile = new SmbFile(remoteUrl);
            if(remoteFile==null){
            System.out.println("共享文件不存在");
            return;
            }
            String fileName = remoteFile.getName();
            File localFile = new File(localDir+File.separator+fileName);
            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
            out = new BufferedOutputStream(new FileOutputStream(localFile));
            byte[] buffer = new byte[1024];
            while(in.read(buffer)!=-1){
                out.write(buffer);
                buffer = new byte[1024];
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
